#include "bigboss.h"
#include "flyer.h"
#include "../weapon/pfire.h"

#include <OgreEntity.h>
#include <stdio.h>
#include <OgreMath.h>
#include <OgreVector3.h>
#include "OgreParticleSystem.h"
#include <OgreParticleEmitter.h>

BigBoss::BigBoss(World *world): BaseEnemy(world)
{

    createModel();
    m_node->scale(50,50,50);
    m_node->yaw(Ogre::Degree(120));

    m_speed = 20;
    m_creationTime = m_world->getCurrentTime();
    m_lastShoot = m_world->getCurrentTime();
    m_health = 20;

    m_movingBack = false; //right moving
}

void BigBoss::colide(AI *object){
    if(object->getType() == TYPE_FLAMER && !object->isNpc()){
        m_health -= object->getDamage();
        Ogre::Vector3 topAdjustment = Ogre::Vector3(0,0,-300);
        this->hit(this->getPosition()+topAdjustment);
        if(m_health <= 0){
            m_logger->m_score += 5;

            std::stringstream logInfo;
            logInfo << "score update, score value," << m_logger->m_score << ", player shot carrier vessel";
            m_logger->log(logInfo.str());

            m_world->setScore(m_logger->m_score);
            m_world->removeActor(this);
            this->animDeath(this->getPosition());
            m_world->removeActor(this);
        }
    }
}

void BigBoss::move(float time){
    Ogre::Vector3 diff = m_world->getPlayer()->getPosition() - m_node->getPosition();

    Ogre::Vector3 shiftSpeed = Ogre::Vector3(100,0,0);
    Ogre::Vector3 leaveSpeed = Ogre::Vector3(100,0,200);

    float elapsed = m_world->getCurrentTime() - m_creationTime;

    std::cout << elapsed << "\n";

    if(elapsed <= 20000){ //after 20'000 ticks, carrier leaves
        if(m_node->getPosition().x >= -1500 && m_movingBack == false){
            // move right
            m_node->setPosition(m_node->getPosition() - shiftSpeed * time);
        }
        else{
            //move left
            m_movingBack = true;
            m_node->setPosition(m_node->getPosition() + shiftSpeed * time);
            if(m_node->getPosition().x >= 1500){
                m_movingBack = false;
            }
        }
    }
    else{
         m_node->setPosition(m_node->getPosition() + leaveSpeed * time);
    }

    m_node->lookAt(m_world->getPlayer()->getPosition(), Ogre::Node::TS_PARENT, Ogre::Vector3::UNIT_Z);

    if(m_world->getCurrentTime() - m_lastShoot > 1500){
        m_world->addActor(new Flyer(m_world), getPosition());
        m_lastShoot = m_world->getCurrentTime();
    }

    diff.normalise();
    diff *= m_speed;

    m_node->setPosition(m_node->getPosition() + time* diff);
}

void BigBoss::animDeath(Ogre::Vector3 lastPos){
    Ogre::SceneManager *scene = m_world->getSceneManager();

    // create particle system and attach it to d_node at death position
    Ogre::ParticleSystem* splatter = scene->createParticleSystem(25);
    splatter->setMaterialName("Examples/Flare");
    splatter->setDefaultDimensions(100, 100);

    Ogre::ParticleEmitter* emitter = splatter ->addEmitter("Point");  // add a point emitter

    // set the emitter properties
    emitter->setAngle(Ogre::Degree(180));
    emitter->setMinTimeToLive(1);
    emitter->setMaxTimeToLive(5);
    emitter->setDuration(0.2);
    emitter->setEmissionRate(2000);
    emitter->setParticleVelocity(500);
    emitter->setDirection(Ogre::Vector3::NEGATIVE_UNIT_Z);
    emitter->setColour(Ogre::ColourValue(0.9,0.8,0,0.5),Ogre::ColourValue(0.7,0.6,0,0.5));

    Ogre::SceneNode *d_node = scene->getRootSceneNode()->createChildSceneNode();
    d_node->setPosition(lastPos);
    d_node->attachObject(splatter);
}

void BigBoss::hit(Ogre::Vector3 lastPos){
    Ogre::SceneManager *scene = m_world->getSceneManager();

    // create particle system and attach it to d_node at death position
    Ogre::ParticleSystem* splatter = scene->createParticleSystem(25);
    splatter->setMaterialName("Examples/Flare");
    splatter->setDefaultDimensions(100, 100);

    Ogre::ParticleEmitter* emitter = splatter ->addEmitter("Point");  // add a point emitter

    // set the emitter properties
    emitter->setAngle(Ogre::Degree(90));
    emitter->setMinTimeToLive(0.3);
    emitter->setMaxTimeToLive(0.5);
    emitter->setDuration(0.05);
    emitter->setEmissionRate(2000);
    emitter->setParticleVelocity(400);
    emitter->setDirection(Ogre::Vector3::NEGATIVE_UNIT_Z);
    emitter->setColour(Ogre::ColourValue::Red);

    Ogre::SceneNode *d_node = scene->getRootSceneNode()->createChildSceneNode();
    d_node->setPosition(lastPos);
    d_node->attachObject(splatter);
}
